import { createApp } from "vue";
import Dialog from "../components/dialog/Dialog.vue";

export default {
  install: (app, options) => {
    // Dialog插件,option为配置选项

    const dialog =  (title, content) => {
        //封装成Promise
      return new Promise((resolve) => {
        const mountNode = document.createElement("div");
        document.body.appendChild(mountNode);


        //优化，回调函数应由外部传入
        const confirm = function () {
          //触发回调
          console.log("触发回调confirm");
          dialogApp.unmount();
          document.body.removeChild(mountNode);
          resolve(true);
        };

        const close = function () {
          console.log("触发关闭回调，卸载组件close");
          //触发关闭回调，卸载组件
          dialogApp.unmount();
          document.body.removeChild(mountNode);
          resolve(false);
        };

        //挂载到div节点上进行显示
        const dialogApp = createApp(Dialog, {
          title,
          content,
          close,
          confirm,
        });
        dialogApp.mount(mountNode);
      });
    };
    app.provide("dialog", dialog);
  },
};
